#include "AspectEngine.h"

namespace agtk {

ShaderPredefines::ShaderPredefines(void)
{
	GLenum glError;

	// Get uniform locations.
	agtk_eyePosition =       g_aspectEngine->GetMainShader()->GetUniform("agtk_eyePosition");       // Works
	agtk_ambientColor =      g_aspectEngine->GetMainShader()->GetUniform("agtk_ambientColor");      // Doesn't work
	agtk_diffuseColor =      g_aspectEngine->GetMainShader()->GetUniform("agtk_diffuseColor");      // Doesn't work
	agtk_falloffColor =      g_aspectEngine->GetMainShader()->GetUniform("agtk_falloffColor");      // Doesn't work
	agtk_specularIntensity = g_aspectEngine->GetMainShader()->GetUniform("agtk_specularIntensity"); // Doesn't work
	agtk_specularPower =     g_aspectEngine->GetMainShader()->GetUniform("agtk_specularPower");     // Doesn't work
	agtk_glowIntensity =     g_aspectEngine->GetMainShader()->GetUniform("agtk_glowIntensity");     // Works
	agtk_falloffIntensity =  g_aspectEngine->GetMainShader()->GetUniform("agtk_falloffIntensity");  // Works
	agtk_falloffPower =      g_aspectEngine->GetMainShader()->GetUniform("agtk_falloffPower");      // Works
	agtk_falloffDetail =     g_aspectEngine->GetMainShader()->GetUniform("agtk_falloffDetail");     // Works
	agtk_diffuseMap =        g_aspectEngine->GetMainShader()->GetUniform("agtk_diffuseMap");        // Works
	agtk_normalMap =         g_aspectEngine->GetMainShader()->GetUniform("agtk_normalMap");         // Works
	agtk_specularMap =       g_aspectEngine->GetMainShader()->GetUniform("agtk_specularMap");       // Works
	agtk_glowMap =           g_aspectEngine->GetMainShader()->GetUniform("agtk_glowMap");           // Works

	glError = glGetError();
	while (glError != GL_NO_ERROR) {
		glError = glGetError();
		switch (glError) {
			case GL_INVALID_ENUM:
				g_aspectEngine->GetLogger()->Log(LOGGER_ERROR, "Invalid enum.");
				break;
			case GL_INVALID_VALUE:
				g_aspectEngine->GetLogger()->Log(LOGGER_ERROR, "Invalid value.");
				break;
			case GL_INVALID_OPERATION:
				g_aspectEngine->GetLogger()->Log(LOGGER_ERROR, "Invalid operation.");
				break;
			case GL_STACK_OVERFLOW:
				g_aspectEngine->GetLogger()->Log(LOGGER_ERROR, "Stack overflow.");
				break;
			case GL_OUT_OF_MEMORY:
				g_aspectEngine->GetLogger()->Log(LOGGER_ERROR, "Out of memory.");
				break;
			case GL_TABLE_TOO_LARGE:
				g_aspectEngine->GetLogger()->Log(LOGGER_ERROR, "Table too large.");
				break;
		}
		glError = glGetError();
	}

	// Get attribute locations.
	agtk_tangent = g_aspectEngine->GetMainShader()->GetAttribute("agtk_tangent");
	agtk_bitangent = g_aspectEngine->GetMainShader()->GetAttribute("agtk_bitangent");

	// Set texture indices.
	agtk_diffuseMap->Set1i(0);
	agtk_normalMap->Set1i(1);
	agtk_specularMap->Set1i(2);
	agtk_glowMap->Set1i(3);

	// Set eye position.
	agtk_eyePosition->Set3f(0.0f, 0.0f, 100.0f);

	g_aspectEngine->GetMainShader()->GetUniform("fvLightPosition")->Set3f(100.0f, 100.0f, -80.0f);
	agtk_ambientColor->Set4f(0.37, 0.37, 0.37, 1.0);
}

ShaderPredefines::~ShaderPredefines(void)
{
	SAFE_DELETE(agtk_eyePosition);
	SAFE_DELETE(agtk_ambientColor);
	SAFE_DELETE(agtk_diffuseColor);
	SAFE_DELETE(agtk_falloffColor);
	SAFE_DELETE(agtk_specularIntensity);
	SAFE_DELETE(agtk_specularPower);
	SAFE_DELETE(agtk_glowIntensity);
	SAFE_DELETE(agtk_falloffPower);
	SAFE_DELETE(agtk_falloffDetail);
	SAFE_DELETE(agtk_diffuseMap);
	SAFE_DELETE(agtk_normalMap);
	SAFE_DELETE(agtk_specularMap);
	SAFE_DELETE(agtk_glowMap);
	SAFE_DELETE(agtk_tangent);
	SAFE_DELETE(agtk_bitangent);
}

}